<?php
namespace app\admin\controller;
use app\model\WxActivityField as WxActivityFieldModel;
use think\Db;


class WxActivityField extends Base
{
    public function __construct(){
        $model = new WxActivityFieldModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '委估对象';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }


    public function save(){
        $wx_activity_id = input('wx_activity_id');
        if (empty($wx_activity_id)) { $this->error('委估对象ID不能为空'); }
        $model = $this->model;
        
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','is_show','is_must','is_readonly','type','params','default'],
            ];
            $params = paramFilter(request()->param(),$fields);
            if (!empty($params['error_msg'])) {
                $this->error($params['error_msg']);
            }

            $save_data = [];
            foreach (array_keys($params['name']) as $k) {
                $save_data[] = [
                    'wx_activity_id'=>$wx_activity_id,
                    'name'=>$params['name'][$k],
                    'is_show'=>$params['is_show'][$k],
                    'is_must'=>$params['is_must'][$k],
                    'is_readonly'=>$params['is_readonly'][$k],
                    'type'=>$params['type'][$k],
                    'params'=>$params['params'][$k],
                    'default'=>$params['default'][$k],
                    'field_sn'=>'field_'.$k
                ];
            }

            $model->where('wx_activity_id',$wx_activity_id)->delete();
            if ( $model->insertAll($save_data) ) {
                $this->success('保存成功',url('save',['wx_activity_id'=>$wx_activity_id]));
            }else{
                $this->error("编辑失败");
            }
        }

        $list = $model->where('wx_activity_id',$wx_activity_id)->order('id','asc')->select()->toarray();
        if ( empty($list) ) {
            $list = $model->getInitList();
        }
        return view('save', [
            'list'=>$list,
            'wx_activity_id'=>$wx_activity_id,
            'types'=>$model->getTypeList()
        ]);
    }




   
}
